<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
class ManufacturersComponent
{
	function ManufacturersComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$language_id = (int)$params['languageId'];

		$cache_id = $language_id;
		$json = read_cache($cache_id, $handle);
		if ($json)
		{
			if ($json == 'null') return null;
			else return json_decode($json, true);
		}

		$value{'ManufacturersComponentMetaData'}{'manufacturers_in_a_list_max'}= (int)MAX_DISPLAY_MANUFACTURERS_IN_A_LIST;
		$value{'ManufacturersComponentMetaData'}{'manufacturers_select_size'}= (int)MAX_MANUFACTURERS_LIST;
		$value{'ManufacturersComponentMetaData'}{'manufacturers_name_len_max'}= (int)MAX_DISPLAY_MANUFACTURER_NAME_LEN;

		$sqlQuery = 'select m.manufacturers_id, m.manufacturers_name, ' .
		'coalesce(m.manufacturers_image, "") as image, coalesce(mi.manufacturers_url, "") as url ' .
		'from ' . TABLE_MANUFACTURERS . ' m ' .
		'left join ' . TABLE_MANUFACTURERS_INFO . ' mi on (m.manufacturers_id = mi.manufacturers_id and mi.languages_id = \'' . $language_id . '\') ' .
		'where m.manufacturers_id = 0';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['manufacturers_id'];
			$value{$handle}{$i}{'name'}= $row['manufacturers_name'];
			$value{$handle}{$i}{'image'}= $row['image'];
			$value{$handle}{$i}{'url'}= $row['url'];
			$i++;		}
		if ($i == 0)
		{
			$value{$handle}{$i}{'id'}= 0;
			$value{$handle}{$i}{'name'}= 'All';
			$value{$handle}{$i}{'image'}= null;
			$value{$handle}{$i}{'url'}= null;
			$i++;
		}

		$sqlQuery = 'select m.manufacturers_id, m.manufacturers_name, ' .
		'coalesce(m.manufacturers_image, "") as image, coalesce(mi.manufacturers_url, "") as url ' .
		'from ' . TABLE_PRODUCTS . ' p, ' . TABLE_MANUFACTURERS . ' m ' .
		'left join ' . TABLE_MANUFACTURERS_INFO . ' mi on (m.manufacturers_id = mi.manufacturers_id and mi.languages_id = \'' . $language_id . '\') ' .
		'where m.manufacturers_id = p.manufacturers_id  and m.manufacturers_id <> 0 ' .
		'group by m.manufacturers_id ' .
		'order by m.manufacturers_name';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['manufacturers_id'];
			$value{$handle}{$i}{'name'}= $row['manufacturers_name'];
			$value{$handle}{$i}{'image'}= $row['image'];
			$value{$handle}{$i}{'url'}= $row['url'];
			$i++;		}
					
		if ($value != null) $json = json_encode($value);
		write_cache($json, $cache_id, $handle);

		return $value;
	}
}
?>
